package com.hrt.manage.product.controllor;

import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.hrt.base.BaseController;
import com.hrt.bean.GiftType;
import com.hrt.bean.GiftTypeDir;
import com.hrt.manage.product.service.GiftTypeDirService;
import com.hrt.manage.product.service.GiftTypeService;

@Controller
@RequestMapping("manage/giftType")
public class GiftTypeControllor extends BaseController<GiftType> {
	
	/**礼品分类*/
	@Autowired
	private GiftTypeService giftTypeService;

	/**礼品分类目录明细*/
	@Autowired
	private GiftTypeDirService giftTypeDirService;
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@RequestMapping(value = "/queryByPage")
	@ResponseBody
	public Map queryByPage(
			@RequestParam(value = "pageIndex", required = false) Integer pageIndex,
			@RequestParam(value = "pageSize", required = false) Integer pageSize,
			HttpServletRequest request) {
		Map resultMap = new HashMap();

		if (pageSize == null || "".equals(pageSize)) {
			pageSize = 30;
		}
		if (pageIndex == null || "".equals(pageIndex)) {
			pageIndex = 1;
		}
		setPageIndex(pageIndex);

		setPageSize(pageSize);

		setTotalCount(giftTypeService.totalCount());

		List<GiftType> resultList = giftTypeService.queryByPage(
				getPageIndex(), getPageSize());
		setList(resultList);
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();

		resultMap.put("pageResult", page);
		return resultMap;
	}

	/**
	 * 查看
	 * 
	 */
	@RequestMapping(value = "/list")
	public ModelAndView list(HttpServletRequest request) {
		ModelAndView view = new ModelAndView();

		list = giftTypeService.queryByPage(getPageIndex(), getPageSize());
		setTotalCount(giftTypeService.totalCount());
		this.setPageCount(this.computePageCount(this.getTotalCount(),
				this.getPageSize()));

		page = this.getPage();
		/** 父级  */
		List<GiftType> typeParList = this.giftTypeService.findDelParents();
		
		view.addObject("typeParList", typeParList);
		view.addObject("pageResult", page);
		return view;

	}
	
	/**
	 * 父级
	 * */
	/*@RequestMapping(value = "/listParents")
	public ModelAndView listParents() {
		ModelAndView view = new ModelAndView();
		List<GiftType> typeParList = this.giftTypeService.findDelParents();
		view.addObject("typeParList", typeParList);
		return view;
	}*/
	
	/**
	 * 子级
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping(value = "/listSon")
	public @ResponseBody Map listSon(
			@RequestParam(value = "typeParId", required = true) String typeParId,
			HttpServletRequest request) {
		Map map = new HashMap();
		if(typeParId !=null || typeParId != ""){
			List<GiftType> typeList = this.giftTypeService.findDelSonById(Long.parseLong(typeParId));
			map.put("typeList", typeList);
		}
		return map;
	}
	
	

	/**
	 * 删除
	 * */
	@RequestMapping(value = "/delete", method = RequestMethod.POST)
	public @ResponseBody Long[] delete(
			@RequestParam(value = "ids", required = true) String ids,
			HttpServletResponse response) throws IOException {
		Long[] idArr = null;
		if (ids != null) {
			if (ids.indexOf(",") != -1) {
				String[] idStrs = ids.split(",");
				idArr = new Long[idStrs.length];
				for (int idx = 0; idx < idStrs.length; idx++) {
					idArr[idx] = Long.parseLong(idStrs[idx]);
					
					/**删除礼品分类明细*/
					List<GiftTypeDir> listGiftTypeDir= this.giftTypeDirService.getGiftTypeDirByGdId(idArr[idx]);
					if(listGiftTypeDir!=null && listGiftTypeDir.size()>0){
						for (Iterator<GiftTypeDir> iterDir = listGiftTypeDir.iterator(); iterDir.hasNext();) {
							GiftTypeDir gtd = (GiftTypeDir) iterDir.next();
							try {
								//删除礼品分类明细
								this.giftTypeDirService.deleteByOneId(gtd.getId());
							} catch (Exception e) {
								e.printStackTrace();
							}
				        }
					}
					
					
				}
			} else {
				idArr = new Long[] { Long.parseLong(ids) };
				/**删除礼品分类明细*/
				List<GiftTypeDir> listGiftTypeDir= this.giftTypeDirService.getGiftTypeDirByGdId(Long.parseLong(ids));
				if(listGiftTypeDir!=null && listGiftTypeDir.size()>0){
					for (Iterator<GiftTypeDir> iterDir = listGiftTypeDir.iterator(); iterDir.hasNext();) {
						GiftTypeDir gtd = (GiftTypeDir) iterDir.next();
						try {
							//删除礼品分类明细
							this.giftTypeDirService.deleteByOneId(gtd.getId());
						} catch (Exception e) {
							e.printStackTrace();
						}
			        }
				}
			}

		}
		try {
			giftTypeService.deleteById(idArr);

			return idArr;
		} catch (Exception e) {
			e.printStackTrace();
			return null;
		}

	}

	

	/**
	 * 添加
	 * */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(@ModelAttribute("giftTypeDir") GiftType gi,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			if(gi.getParentId()>0){
				gi.setGiftType(this.giftTypeService.getById(gi.getParentId()));
			}else{
				gi.setGiftType(null);
			}
			giftTypeService.addGiftType(gi);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	/**
	 * 跳转到修改页面
	 * */
	@SuppressWarnings("rawtypes")
	@RequestMapping(value = "/show")
	@ResponseBody
	public Map show(@RequestParam(value = "id", required = true) String giId,
			HttpServletRequest request, HttpServletResponse response) {

		Map map = new HashMap();
		if (giId != null && !"".equals(giId)) {
			GiftType gi = this.giftTypeService.getById(new Long(giId));
			if (gi != null) {
				map = transBean2Map(gi);
			}
		}
		return map;
	}

	/**
	 * 修改
	 * */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public void update(@ModelAttribute("giftTypeDir") GiftType gi,
			HttpServletRequest request, HttpServletResponse response) {
		try {
			if(gi.getParentId()>0){
				gi.setGiftType(this.giftTypeService.getById(gi.getParentId()));
			}else{
				gi.setGiftType(null);
			}
			giftTypeService.updateGiftType(gi);
			response.sendRedirect("list.do");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
